<template>
	<view class="container">
		<sub-navigationbar text="优惠券领取中心" btnText=""></sub-navigationbar>
		<view class="coupon" v-for="i in pageData" :key="i.id">
			<view class="coupon-body">
				<view class="price">
					<template v-if="i.discountType!=='免费'">
						<text class="unit" v-if="i.discountType!=='折扣'">¥</text>
						<text class="num">{{i.discountNumber}}</text>
						<text class="discount" v-if="i.discountType=='折扣'">折</text>
					</template>
					<text v-else class="num">{{i.discountType}}</text>
				</view>
				<text class="condition" v-if="i.discountType=='满减'">满{{i.thresholdMoney}}元可用</text>
			</view>
			<view class="coupon-foot">
				<view class="line-group">
					<text class="label">{{i.discountType}}券</text>
					<text class="detail">{{i.discountType=='免费'?'免费一次':""}}</text>
				</view>
				<view class="line-group">
					<text class="label">使用期限</text>
					<text class="detail">{{i.startTime | dateFormat}}-{{i.endTime | dateFormat}}</text>
				</view>
			</view>
		</view>
		<view class="btn-group">
			<button class="btn" type="primary" hover-class="btn-hover" @click="receiveCoupon">点击领取</button>
		</view>
	</view>
</template>

<script>
	import subNavigationbar from '@/components/sub-navigationbar.vue';
	const appData = getApp().globalData;
	export default {
		components: {
			subNavigationbar
		},
		data() {
			return {
				pageData:null,
				couponId:null,
				openId:null,
				pid:null,
			};
		},
		filters:{
			dateFormat(v) {
				return v.split('T')[0].replace(/-/g,"/");
			},
		},
		onLoad() {
			let url = window.location.href;
			let params = url.split('?')[1];
			if(params) {
				let arr = params.split('&');
				params = {};
				arr.forEach(val=>{
					let item = val.split('=');
					let value = item[1].split('#')[0]
					params = {...params,[item[0]]:value}
				})
			}
			console.log('params=>',params)
			let {couponId,openId,pid} = params;
			this.couponId = couponId;
			this.openId = openId;
			this.pid = pid;
		},
		mounted() {
			this.getData()
		},
		methods:{
			// 获取优惠券信息
			getData() {
				uni.showLoading({
					title:'加载中...',
				})
				uni.request({
					url:appData.baseUrl+appData.api.getCouponInfo+`?queryValue=id:${this.couponId}`,
					method:"GET",
					success:(res)=>{
						uni.hideLoading();
						let toastIcon = 'none';
						if(res.data.normal==1) {
							console.log(res.data)
							this.pageData = res.data.list;
						}
						else {
							uni.showToast({
								icon:toastIcon,
								title:res.data.msg
							})
						}
					}
				})
			},
			// 领取
			receiveCoupon() {
				uni.showLoading({
					title:'加载中...',
				})
				let data = {
					openId:this.openId,
					couponId:this.couponId,
					pid:this.pid,
				}
				uni.request({
					url:appData.baseUrl+appData.api.couponReceive,
					data:data,
					header:{"content-type":"application/x-www-form-urlencoded"},
					method:"POST",
					success:(res)=>{
						uni.hideLoading();
						let toastIcon = 'none';
						// if(res.data.normal==1) {
						// 	toastIcon = 'success';
						// }
						uni.showToast({
							icon:toastIcon,
							title:res.data.msg
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.container {
	display: flex; 
	flex-direction: column;
	align-items: center;
}
.coupon {
	margin: 8rpx 0 136rpx;
	width: 696rpx;
	height: 604rpx;
	background:url(../../static/coupon_bg.png) no-repeat center center/100% 100%;
	padding: 120rpx 72rpx 48rpx 68rpx;
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	align-items: center;
	line-height: 1;
	.coupon-body {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: $xy-color-default;
		.price {
			display: flex;
			justify-content: center;
			align-items: center;
			.unit,.discount {
				font-size: 80rpx;
				transform: translateY(40rpx);
				font-weight: bold;
			}
			.num {
				font-size: 180rpx;
				font-weight: bold;
				margin: 0 8rpx;
			}
		}
		.condition {
			font-size: 40rpx;
			margin-top: 24rpx;
		}
	}
	.coupon-foot {
		width: 100%;
		.line-group {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 14rpx;
			line-height: 44rpx;
			font-size: 32rpx;
			color: #fff;
			.label {
				color: #B6DAF7;
			}
		}
	}
}
.btn-group {
	.btn {
		font-size: 28rpx;
		line-height: 4;
		background-color: $xy-color-default;
		border-radius: 50rpx;
		padding: 0 230rpx;
		font-weight: bold;
		&.btn-hover {
			color: hsla(0,0%,100%,0.6);
			background-color: $xy-color-primary-hover;
		}
	}
}
</style>
